package wellness.activities.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import wellness.activities.alert.Agendador;
import wellness.activities.beans.AgendamentoBean;
import wellness.activities.beans.AtividadeBean;
import wellness.activities.beans.LoadBeans;
import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.widget.Toast;

public class AgendamentoData {
	private static String TABLE_NAME = "Agendamento";

	private Context context;
	private SQLiteDatabase db;

	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into "
			+ TABLE_NAME
			+ "(ATIV_ID, AGEN_DATA, AGEN_PRATICOU, AGEN_INTENSIDADE, AGEN_DURACAO) values (?, ?, ?, ?, ?)";

	public AgendamentoData(Context context) {
		this.context = context;
	}

	public long insert(AgendamentoBean a) {
		this.db = DataBase.getDB(this.context);
		this.insertStmt = this.db.compileStatement(INSERT);
		this.insertStmt.bindLong(1, a.getAtividade().getId());
		this.insertStmt.bindString(2, new SimpleDateFormat("dd/MM/yyyy-HH:mm")
				.format(a.getInicio().getTime()));
		this.insertStmt.bindLong(3, -1);
		this.insertStmt.bindNull(4);
		this.insertStmt.bindLong(5, 0);
		long c = this.insertStmt.executeInsert();
		this.selectAll();
		return c;
	}

	public int deleteAll() {
		this.db = DataBase.getDB(this.context);
		int i = this.db.delete(TABLE_NAME, null, null);
		DataBase.closeDB();
		return i;
	}

	public int delete(AgendamentoBean a) {
		this.db = DataBase.getDB(this.context);
		;
		int i = this.db.delete(TABLE_NAME, "ATIV_ID = ?", new String[] { a
				.getId().toString() });
		DataBase.closeDB();
		return i;

	}

	public int finalizar(AgendamentoBean a) {
		this.db = DataBase.getDB(this.context);;
		ContentValues v = new ContentValues();
		v.put("AGEN_PRATICOU", a.isPraticou() ? 1 : 0);
		if (a.isPraticou()) {
			v.put("AGEN_INTENSIDADE", a.getIntensidade());
			v.put("AGEN_DURACAO", a.getDuracao());
		}
		int i = this.db.update(TABLE_NAME, v, "AGEN_ID = ?", new String[] { a
				.getId().toString() });
		DataBase.closeDB();
		return i;

	}

	public void selectAll() {
		this.db = DataBase.getDB(this.context);
		ArrayList<AgendamentoBean> agendamentos = new ArrayList<AgendamentoBean>();
		Cursor cursor = this.db
				.query(TABLE_NAME,
						new String[] { "AGEN_ID, ATIV_ID, AGEN_DATA, AGEN_PRATICOU,  AGEN_DURACAO,  AGEN_INTENSIDADE" },
						null, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				AgendamentoBean agendamento = new AgendamentoBean(
						cursor.getInt(0), LoadBeans.getAtividadePorId(cursor
								.getInt(1)), cursor.getString(2),
						cursor.getInt(3), cursor.getInt(4), cursor.getString(5));
				agendamentos.add(agendamento);

				if (agendamento.isPraticou() == null
						&& new GregorianCalendar().before(agendamento
								.getInicio())) {
					Intent servico = new Intent("AGENDADOR");
					servico.putExtra("AGEN_ID", agendamento.getId());
					context.startService(servico);
				}

			} while (cursor.moveToNext());
		}
		if (cursor != null) {
			cursor.close();
		}
		LoadBeans.setAgendamentos(agendamentos);
		DataBase.closeDB();
	}
}
